package com.openingedu.mba.screen.fragment;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragment;
import com.lurencun.android.support.widget.CommonAdapter;
import com.lurencun.android.toolkit.util.ActivitySwitcher;
import com.openingedu.mba.R;
import com.openingedu.mba.common.AppSetting;
import com.openingedu.mba.entity.MenuVO;
import com.openingedu.mba.screen.activity.practice.WelcomeActivity;
import com.openingedu.mba.screen.adapter.MenuAdapter;
import com.openingedu.mba.screen.adapter.MenuLoader;

public class FragmentPractice extends SherlockFragment {
	
	private Context mContext;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.ft_practice, container, false);
		mContext = view.getContext();
		bindView(view);
		return view;
	}

	private void bindView(View view) {
		GridView grid = (GridView) view.findViewById(R.id.menu_grid);
		CommonAdapter<MenuVO> adapter = new MenuAdapter(mContext);
		final List<MenuVO> data = new MenuLoader(mContext).load(R.array.menu_config);
		adapter.updateDataCache(data);
		grid.setAdapter(adapter);
		
		grid.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				MenuVO menu = data.get(position);
				String[] keys = new String[] { AppSetting.MENU_VALUE, AppSetting.MENU_TITLE, AppSetting.MENU_TARGETNAME };
				Object[] vals = new Object[] { menu.value, menu.title, menu.targetName };
				if (menu.type == 0) {
					ActivitySwitcher.switchTo(getSherlockActivity(), WelcomeActivity.class, keys, vals);
				}
				else if (menu.type == -1) {
					Toast.makeText(mContext, "即将推出，敬请期待", Toast.LENGTH_LONG).show();
				}
				else if (menu.type == 1) {
					ActivitySwitcher.switchTo(getSherlockActivity(), menu.target, keys, vals);
				}
			}

		});
	}
	
}
